<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
	
	<xsl:import href="toXMIFunction.xsl"/>
	<xsl:import href="toXMI.xsl" />
	
	<xsl:variable name="version" select="//XMI/@xmi.version" />
	<xsl:variable name="isAssociation" select="false()" />
	
	<xsl:template match="/">
		<xsl:apply-imports />
	</xsl:template>
	
	<!-- ________________________________________________________________________________________________________ -->
	<!--												Les TEMPLATES									       -->
	<!-- ________________________________________________________________________________________________________ -->

	<xsl:template match="*" mode="Name">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:value-of select="ModelElement.name" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="@name" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@name" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*" mode="IsAbstract">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:value-of select="GeneralizableElement.isAbstract/@xmi.value" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="@isAbstract"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@isAbstract"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*" mode="Visibility">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:apply-templates select="ModelElement.visibility" mode="Value" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="@visibility" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@visibility" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*" mode="Stereotype">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:variable name="id"><xsl:apply-templates select="." mode="Id" /></xsl:variable>
				<xsl:value-of select="//Stereotype/Stereotype.extendedElement/ModelElement[@xmi.idref=$id]/../../ModelElement.name" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="ModelElement.stereotype/Stereotype/@name"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="id"><xsl:value-of select="ModelElement.stereotype/Stereotype/@xmi.idref" /></xsl:variable>
				<xsl:value-of select="//Stereotype[@xmi.id=$id]/@name" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*" mode="Type">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:apply-templates select="." mode="Type_v10" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:apply-templates select="." mode="Type_v11" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="." mode="Type_v10" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*" mode="Type_v10">
		<xsl:variable name="id">
			<xsl:choose>
				<xsl:when test="self::Operation">
					<xsl:apply-templates select="BehavioralFeature.parameter/Parameter/Parameter.kind[@xmi.value='return']/../Parameter.type/DataType"  mode="IdRef"/>
				</xsl:when>
				<xsl:when test="self::Parameter">
					<xsl:apply-templates select="Parameter.type/DataType" mode="IdRef"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="StructuralFeature.type/DataType | StructuralFeature.type/Classifier" mode="IdRef"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="name"><xsl:apply-templates select="//*[@xmi.id=$id]" mode="Name" /></xsl:variable>
		
		<xsl:choose>
			<!-- si le type n'est pas défini -->
			<xsl:when test="$name = '' or $name = '&amp;lt;undefined&amp;gt;'">
				<xsl:text>void</xsl:text>
			</xsl:when>
			<!-- si le type est défini -->
			<xsl:otherwise>
				<xsl:value-of select="$name" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*" mode="Type_v11">
		<xsl:variable name="id">
			<xsl:choose>
				<xsl:when test="self::Operation">
					<xsl:apply-templates select="BehavioralFeature.parameter/Parameter[@kind='return']/Parameter.type/Classifier"  mode="IdRef"/>
				</xsl:when>
				<xsl:when test="self::Parameter">
					<xsl:apply-templates select="Parameter.type/Classifier" mode="IdRef"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="StructuralFeature.type/Classifier" mode="IdRef"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:choose>
			<!-- si le type n'est pas défini -->
			<xsl:when test="//*[@xmi.id=$id]/@name = '' or //*[@xmi.id=$id]/@name= '&amp;lt;undefined&amp;gt;'">
				<xsl:text>void</xsl:text>
			</xsl:when>
			<!-- si le type est défini -->
			<xsl:otherwise>
				<xsl:value-of select="//*[@xmi.id=$id]/@name"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*" mode="IsEntryParameter">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:if test="Parameter.kind/@xmi.value='in' or Parameter.kind/@xmi.value='inout'"><xsl:value-of select="true()"/></xsl:if>
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:if test="@kind='in' or @kind='inout'"><xsl:value-of select="true()"/></xsl:if>
			</xsl:when>
			<xsl:otherwise>
				<xsl:if test="@kind='in' or @kind='inout'"><xsl:value-of select="true()"/></xsl:if>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="AssociationEnd" mode="Aggregation">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:value-of select="AssociationEnd.aggregation/@xmi.value" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="@aggregation" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@aggregation" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="Generalization" mode="Generalization.child_idRef">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:value-of select="Generalization.child//@xmi.idref" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="@subtype" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="Generalization.child//@xmi.idref" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="Generalization" mode="Generalization.parent_idRef">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:value-of select="Generalization.parent//@xmi.idref" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="@supertype" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="Generalization.parent//@xmi.idref" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="Dependency" mode="Abstraction.client_idRef">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:value-of select="Dependency.child//@xmi.idref" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="@client" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@client" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="Dependency" mode="Abstraction.supplier_idRef">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:value-of select="Dependency.parent//@xmi.idref" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="@supplier" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@supplier" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="AssociationEnd" mode="Association.participant_idRef">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:value-of select="AssociationEnd.type//@xmi.idref" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="@type" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="AssociationEnd.participant//@xmi.idref" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="Dependency" mode="Implementation">
		<xsl:element name="Implementation">
			<xsl:call-template name="AttributeName">
				<xsl:with-param name="node" select="."/>
			</xsl:call-template>
			<xsl:call-template name="AttributeDocumentation">
				<xsl:with-param name="node" select="."/>
			</xsl:call-template>
			<xsl:call-template name="AttributeClientClassName">
				<xsl:with-param name="node" select="."/>
			</xsl:call-template>
			<xsl:call-template name="AttributeSupplierClassName">
				<xsl:with-param name="node" select="."/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="Association | AssociationClass" mode="xmiMultiplicity">
		<xsl:param name="role"/>
				
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:apply-templates select="Association.connection/AssociationEnd[position()=$role]" mode="MultiplicityLower"/>
				<xsl:text>..</xsl:text>
				<xsl:apply-templates select="Association.connection/AssociationEnd[position()=$role]" mode="MultiplicityUpper"/>
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:value-of select="Association.connection/AssociationEnd[position()=$role]/@multiplicity"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="Association.connection/AssociationEnd[position()=$role]" mode="MultiplicityLower"/>
				<xsl:text>..</xsl:text>
				<xsl:apply-templates select="Association.connection/AssociationEnd[position()=$role]" mode="MultiplicityUpper"/>
			</xsl:otherwise>
		</xsl:choose>
		
	</xsl:template>
	
	<xsl:template match="AssociationEnd" mode="MultiplicityLower">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:choose>
					<xsl:when test="AssociationEnd.multiplicity/Multiplicity/Multiplicity.range/MultiplicityRange/MultiplicityRange.upper='-1'">
						<xsl:text>*</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="AssociationEnd.multiplicity/Multiplicity/Multiplicity.range/MultiplicityRange/MultiplicityRange.lower" />
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="AssociationEnd.multiplicity/Multiplicity/Multiplicity.range/MultiplicityRange/@upper='-1'">
						<xsl:text>*</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="AssociationEnd.multiplicity/Multiplicity/Multiplicity.range/MultiplicityRange/@lower" />
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="AssociationEnd" mode="MultiplicityUpper">
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:choose>
					<xsl:when test="AssociationEnd.multiplicity/Multiplicity/Multiplicity.range/MultiplicityRange/MultiplicityRange.upper='-1'">
						<xsl:text>*</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="AssociationEnd.multiplicity/Multiplicity/Multiplicity.range/MultiplicityRange/MultiplicityRange.upper"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="AssociationEnd.multiplicity/Multiplicity/Multiplicity.range/MultiplicityRange/@upper='-1'">
						<xsl:text>*</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="AssociationEnd.multiplicity/Multiplicity/Multiplicity.range/MultiplicityRange/@upper"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<!-- ****************** template attribute 'CLIENTCLASSNAME' ****************** -->	
	<xsl:template match="*" mode="GetAssociationClassifier">
		<xsl:choose>
      <xsl:when test="$version='1.2'">
        <xsl:value-of select="@name" />
      </xsl:when>
			<xsl:when test="$version='1.0'">
				<xsl:apply-templates select="." mode="Name" />
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:variable name="id" select="ModelElement.taggedValue/TaggedValue[@tag = 'associationclass']/@value" />
				<xsl:apply-templates select="//*[@xmi.id=$id]" mode="Name" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="idTaggedValue" select="//TagDefinition[@name='associationclass']/@xmi.id" />
				<xsl:variable name="id" select="ModelElement.taggedValue/TaggedValue//TagDefinition[@xmi.idref=$idTaggedValue]/../../TaggedValue.dataValue" />
				<xsl:apply-templates select="//*[@xmi.id=$id]" mode="Name" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template name="IsAssociationClass">
		<xsl:param name="id" />
		<xsl:choose>
			<xsl:when test="//Association/ModelElement.taggedValue/TaggedValue[@tag = 'associationclass' and @value= $id]">
				<xsl:value-of select="true()"/>
			</xsl:when>
      <xsl:when test="local-name()='AssociationClass'">
        <xsl:value-of select="true()"/>
      </xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="false()"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template name="IsAssociationClassifier">
		<xsl:param name="node" />
		<xsl:choose>
			<xsl:when test="$version='1.0'">
				<xsl:choose>
					<xsl:when test="$node/ModelElement.taggedValue/TaggedValue[@tag = 'associationclass']"><xsl:value-of select="true()"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="false()"/></xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$version='1.1'">
				<xsl:choose>
					<xsl:when test="$node/ModelElement.taggedValue/TaggedValue[@tag = 'associationclass']"><xsl:value-of select="true()"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="false()"/></xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="id" select="//TagDefinition[@name='associationclass']/@xmi.id" />
				<xsl:choose>
					<xsl:when test="$node/ModelElement.taggedValue/TaggedValue//TagDefinition[@xmi.idref=$id]"><xsl:value-of select="true()"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="local-name($node)='AssociationClass'"/></xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
</xsl:stylesheet>